package code.c12;


/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/12/1 10:21
 * @package code.c12
 * @description 文件说明
 */

public class Ticket {
    public int nums = 100;

    Ticket(int nums){
        this.nums = nums;
    }

    Ticket(){

    }

    public int getNums() {
        return nums;
    }

    public int sellone(){
        nums --;
        return nums;
    }
}
class TicketThread extends Thread{
    Ticket ticket;
    TicketThread(Ticket ticket,String name){
        super(name);
        this.ticket = ticket;
    }

    @Override
    public void run() {
        while (true){
            try {
                sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (ticket){
                if (ticket.nums > 0){
                    int sellone = ticket.sellone();
                    System.out.println(Thread.currentThread().getName() + "卖出了一张飘，还剩余" + sellone + "张票");
                }
            }
        }
    }
}

class TestTickets{
    static  Ticket ticket = new Ticket(100);

    public static void main(String[] args) {
        TicketThread threada = new TicketThread(ticket, "站点A");
        TicketThread threadb = new TicketThread(ticket, "站点B");

        threada.start();
        threadb.start();

    }
}

